using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Dorm.Helpers;

namespace Dorm.Maps
{
    public class ComponentMap<T>: IEntityMap
    {
        public List<MappedProperty> MappedProperties { get; set; }

        public ComponentMap()
        {
            MappedProperties = new List<MappedProperty>();
        }

        public MapOption Map(Expression<Func<T, object>> propertyName)
        {
            string strPropertyName = LambdaParser.GetFieldName<T>(propertyName);

            var mappedProperty = new MappedProperty
                                     {
                                         PropertyName = strPropertyName,
                                         PropertyType = typeof(T).GetProperty(strPropertyName).PropertyType,
                                         MappingType = MappedProperty.MappingTypes.Simple,
                                     };

            MappedProperties.Add(mappedProperty);

            return new MapOption(mappedProperty);
        }

        public void References(Expression<Func<T, object>> propertyName)
        {
            string strPropertyName = LambdaParser.GetFieldName<T>(propertyName);

            var mappedProperty = new MappedProperty
            {
                PropertyName = strPropertyName,
                PropertyType = typeof(T).GetProperty(strPropertyName).PropertyType,
                MappingType = MappedProperty.MappingTypes.Reference,
            };

            MappedProperties.Add(mappedProperty);
        }
    }
}